﻿using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace orm
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using (var db = new MyDbContext())
            {
                // 添加
                //db.TUsers.Add(new TUser { Name = "张三", Age = 25, Mail = "zhangsan@example.com", Sex = "男", Addr = "北京市", Phone = "13800000000" });
                //db.SaveChanges();

                //Console.WriteLine("添加成功");


                // 构造批量插入数据
                //var users = new List<TUser>
                //{
                //    new TUser { Name = "用户A", Age = 20, Mail = "a@example.com", Sex = "男", Addr = "北京", Phone = "13800000001" },
                //    new TUser { Name = "用户B", Age = 22, Mail = "b@example.com", Sex = "女", Addr = "上海", Phone = "13800000002" }
                //};

                //using (var tran = db.Database.BeginTransaction())
                //{
                //    try
                //    {
                //        db.TUsers.AddRange(users);
                //        db.SaveChanges();
                //        tran.Commit();
                //        Console.WriteLine("批量插入成功");
                //    }
                //    catch (Exception ex)
                //    {
                //        tran.Rollback();
                //        Console.WriteLine("批量插入失败: " + ex.Message);
                //    }
                //}

                // 查询
                //var users = db.TUsers.ToList();
                //Console.WriteLine("查询所有用户：");
                //foreach (var u in users)
                //{
                //    Console.WriteLine($"{u.Id}, {u.Name}, {u.Age}");
                //}


                // 条件查询：年龄大于20且性别为男
                var users = db.TUsers
                    .Where(u => u.Age > 20 && u.Sex == "男")
                    .ToList();

                Console.WriteLine("条件查询结果：");
                foreach (var u in users)
                {
                    Console.WriteLine($"{u.Id}, {u.Name}, {u.Age}, {u.Sex}");
                }

                // 修改
                //var firstUser = db.TUsers.FirstOrDefault();
                //if (firstUser != null)
                //{
                //    firstUser.Name = "李四";
                //    db.SaveChanges();
                //    Console.WriteLine("修改成功");
                //}

                // 删除
                //var deleteUser = db.TUsers.FirstOrDefault(u => u.Name == "李四");
                //if (deleteUser != null)
                //{
                //    db.TUsers.Remove(deleteUser);
                //    db.SaveChanges();
                //    Console.WriteLine("删除成功");
                //}
            }
            //Console.WriteLine("Hello, World!");
        }
    }

    public class MyDbContext : DbContext
    {
        public DbSet<TUser> TUsers { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySql(
                "server=localhost;user=yejing;password=123456;database=test;",
                new MySqlServerVersion(new Version(8, 0, 36))
            );
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<TUser>().ToTable("tuser");
        }
    }

    [Table("tuser")]
    public class TUser
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Mail { get; set; }
        public string Sex { get; set; }
        public string Addr { get; set; }
        public string Phone { get; set; }
    }
}
